package com.zhoukai.config.shiro;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhoukai.domain.Menu;
import com.zhoukai.domain.Role;
import com.zhoukai.domain.User;
import com.zhoukai.service.MenuService;
import com.zhoukai.service.RoleService;
import com.zhoukai.service.UserService;
import com.zhoukai.vo.ActiverUser;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import redis.clients.jedis.StreamGroupInfo;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 自定义realm去匹配用户名和密码
 * @Description TODO
 * @Author Mr.ZHOU
 * @Date Created in 2021/12/15 15:37
 */
public class UserRealm extends AuthorizingRealm {
    @Autowired
    @Lazy
    private UserService userService;
    @Autowired
    @Lazy
    private RoleService roleService;
    @Autowired
    @Lazy
    private MenuService menuService;
    public String getName() {
        return this.getClass().getSimpleName();

    }



    /**
     *  做认证  --就是登陆
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String phone = token.getPrincipal().toString();
        User user = userService.queryUserByPhone(phone);
        if (user == null) {
            return null;// shiro 会自动的处理NULL
        }

        ActiverUser activerUser = new ActiverUser();
        activerUser.setUser(user);
        // 注入用户的角色
        List<Role> roles = roleService.getRoleIdsByUserId(user.getUserId());
        if (CollectionUtil.isNotEmpty(roles)) {
            List<String> roleCodes = roles.parallelStream().map(Role::getRoleCode).collect(Collectors.toList());
            activerUser.setRoles(roleCodes);// ? 怎么查询用户的角色
        }
        List<Menu> perms = menuService.getPermsByUserId(user.getUserId()) ;
        if(CollectionUtil.isNotEmpty(perms)){
           List<String> percode = perms.parallelStream().map(Menu::getPercode).collect(Collectors.toList());
           activerUser.setPermissions(percode);// ? 怎么查询用户的角色
       }
        return new SimpleAuthenticationInfo(activerUser, user.getPassword(), ByteSource.Util.bytes(user.getSalt().getBytes()), phone);

    }


    /**
     * 做授权 登陆成功之后判断用户是否有某个菜单或按钮的权限
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        //身份得到的就是上一个方法的返回值的值 第一个参数activerUser
        ActiverUser activerUser= (ActiverUser) principals.getPrimaryPrincipal();
        SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
        return info;

    }

}
